/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.comment.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.bsl.comments.application.CommentService;
import com.foreveross.bsl.comments.application.vo.CommentFeedbackVo;
import com.foreveross.bsl.comments.application.vo.CommentVo;
import com.foreveross.bsl.common.utils.web.QueryFilter;
import com.foreveross.bsl.common.utils.web.QuerySpecs;
import com.foreveross.bsl.common.utils.web.model.PageMustache;
import com.foreveross.bsl.common.utils.web.model.QueryModelHelper;
import com.foreveross.bsl.common.utils.web.model.SelectBoxModel;
import com.foreveross.bsl.common.utils.web.model.SortInfoModel;
import com.foreveross.bsl.mam.app.application.AppMainApplication;
import com.foreveross.bsl.mam.app.application.vo.AppMainVo;
import com.foreveross.bsl.mam.rbac.application.UserApplication;
import com.foreveross.bsl.mam.rbac.application.vo.UserVO;
import com.foreveross.bsl.web.BaseController;

/**
 * xxx
 * 
 * @author Wangyi
 * @version v1.0
 * 
 * @date 2013-7-17
 * 
 */
@Controller
@RequestMapping(value = "/comment")
public class CommentController extends BaseController {

	@Inject
	private CommentService cmtService;

	@Inject
	private UserApplication userService;

	@Inject
	private AppMainApplication appService;

	@RequestMapping(value = "")
	public String listByAppId(
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "appId", required = false) String appId,
			Model model, HttpServletRequest request) {
		String queryUrl = "/comment";
		QueryModelHelper queryHelper = new QueryModelHelper(queryUrl,
				makeQueryFields(), makeSortInfo());
		QuerySpecs querySpecs = queryHelper.populateQueryModel(model, request);
		if (StringUtils.isNotBlank(appId)) {
			querySpecs.addFilter(QueryFilter.eq("appId", appId));
		}
		Page<CommentVo> cmts = cmtService.findComments(pageNumber, pageSize,
				querySpecs);
		model.addAttribute("comments", cmts);
		model.addAttribute("appId", appId);
		model.addAttribute("page", new PageMustache(cmts, PAGINATION_SIZE));
		model.addAttribute("status", this.makeStatusModel(querySpecs));
		return "comment/list";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model,
			@RequestParam(value = "appId", required = false) String appId) {
		List<AppMainVo> apps = this.appService.findAll();
		model.addAttribute("comment", new CommentVo());
		List<UserVO> users = userService.findAll();
		model.addAttribute("users", users);
		model.addAttribute("apps", apps);
		return "comment/comment-form";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(CommentVo comment,
			@RequestParam(value = "appId", required = false) String appId,
			RedirectAttributes redirectAttributes) {
		this.cmtService.addComment(comment);
		redirectAttributes.addFlashAttribute("message", "创建意见成功");
		redirectAttributes.addAttribute("appId", appId);
		return "redirect:/comment";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") String id,
			@RequestParam(value = "appId", required = false) String appId,
			RedirectAttributes redirectAttributes) {
		this.cmtService.deleteComment(id);
		redirectAttributes.addFlashAttribute("message", "删除意见成功");
		redirectAttributes.addAttribute("appId", appId);
		return "redirect:/comment";
	}

	@RequestMapping(value = "feedback", method = RequestMethod.GET)
	public String listFeedback(
			@RequestParam("commentId") String commentId,
			@RequestParam(value = "page", defaultValue = "1") int pageNumber,
			@RequestParam(value = "page.size", defaultValue = PAGE_SIZE) int pageSize,
			@RequestParam(value = "appId", required = false) String appId,
			Model model) {
		Page<CommentFeedbackVo> feedbacks = this.cmtService
				.findCommentFeedbacks(commentId, pageNumber, pageSize,
						QuerySpecs.EMPTY_QUERY_SPECS);
		model.addAttribute("feedbacks", feedbacks);
		model.addAttribute("commentId", commentId);
		model.addAttribute("appId", appId);
		model.addAttribute("page", new PageMustache(feedbacks, PAGINATION_SIZE));

		return "comment/feedbacks";
	}

	@RequestMapping(value = "feedback", method = RequestMethod.POST)
	public String feedback(@RequestParam("commentId") String commentId,
			@RequestParam("content") String content,
			@RequestParam(value = "appId", required = false) String appId,
			RedirectAttributes redirectAttributes) {
		String username = this.getCurrentUser().getUsername();
		this.cmtService.feedbackComment(commentId, content, username);
		redirectAttributes.addAttribute("commentId", commentId);
		redirectAttributes.addAttribute("appId", appId);
		return "redirect:/comment/feedback";
	}

	/**
	 * 通过commentid查找相关的意见
	 * 
	 * @param commentId
	 * 
	 * @return CommentVo
	 */
	@ResponseBody
	@RequestMapping(value = "getComment", method = RequestMethod.GET)
	public CommentVo getComment(String commentId) {
		return cmtService.getById(commentId);
	}

	/**
	 * 编辑意见信息
	 * 
	 * @param user
	 * 
	 * @return Map<String,String>
	 */
	@ResponseBody
	@RequestMapping("editComment")
	public Map<String, String> editComment(
			@RequestParam("id") String commentId,
			@RequestParam("appId") String appId,
			@RequestParam("status") String status) {
		Map<String, String> _result = new HashMap<String, String>();

		CommentVo comment = cmtService.getById(commentId);
		comment.setStatus(CommentVo.CommentStatusEnum.valueOf(status));
		cmtService.updateEntity(comment);
		_result.put("result", "success");
		return _result;
	}

	@Override
	protected String getModuleMenuName() {
		return "云服务";
	}

	private SortInfoModel makeSortInfo() {
		SortInfoModel sim = new SortInfoModel();
		sim.setSortEntry("createTime", "创建时间", false);
		return sim;
	}

	private SelectBoxModel makeQueryFields() {
		SelectBoxModel sbm = new SelectBoxModel();
		sbm.addOption("LIKE_loginName", "登录名");
		sbm.addOption("LIKE_content", "内容");
		return sbm;
	}

	private SelectBoxModel makeStatusModel(QuerySpecs querySpecs) {
		SelectBoxModel sbm = new SelectBoxModel();
		sbm.addOption("", "所有状态");
		for (CommentVo.CommentStatusEnum st : CommentVo.CommentStatusEnum
				.values()) {
			sbm.addOption(st.name(), st.getDescription());
		}
		QueryFilter qf = querySpecs.getFilter("status");
		if (qf != null) {
			sbm.setOptionSelected(qf.getValue().toString());
		}
		return sbm;
	}

}
